home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / c / AMesaRTL.lha / Mesa-2.6 / amiga / drivers / AmigaMesaRTL / amigamesartl.c next >
C/C++ Source or Header  |  1998-09-19  |  50KB  |  2,008 lines

  1.  
  2. /*
  3.  * Mesa 3-D graphics library
  4.  * Version:  2.3
  5.  * Copyright (C) 1995-1997  Brian Paul
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Library General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Library General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Library General Public
  18.  * License along with this library; if not, write to the Free
  19.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  */
  21.  
  22.  
  23. /*
  24.  * amigamesartl.c
  25.  *
  26.  * Version 1.0  27 Jun 1998
  27.  * by Jarno van der Linden
  28.  * jarno@kcbbs.gen.nz
  29.  *
  30.  * Based on ddsample.c ver 1.5
  31.  *
  32.  * Version 1.1  02 Aug 1998
  33.  * by Jarno van der Linden
  34.  * jarno@kcbbs.gen.nz
  35.  *
  36.  * - Fixed several bugs in glClear()
  37.  * - QUICKLOOP macros added
  38.  * - Noticed that { *x = p; x++; } is faster than { *x++ = p; }
  39.  * - Quantizer changed to plugin library
  40.  * - Use environment variables to select quantizer
  41.  *
  42.  * Version 2.0  19 Sep 1998
  43.  * by Jarno van der Linden
  44.  * jarno@kcbbs.gen.nz
  45.  *
  46.  * - (Get|Set)IndexRGB added
  47.  * - Set and get attributes added
  48.  * - Lots more tags handled
  49.  * - Added palette table for index mode
  50.  * - Demands at least v2 quantizers
  51.  * - Removed colourbase correction from index functions
  52.  * - Deprecated some tags replaced by QNTZR_#?
  53.  * - Changed CreateContext interface
  54.  * - Added Szymon Ulatowski's resizing changes
  55.  * - Changed "Quantizer" to "OutputHandler"
  56.  * - Automagic mesamain.library opening added
  57.  * - AMRTL_SupportsOH tag added
  58.  * - RGBA and ARGB byte order supported
  59.  * - RGBA byte order determined by OH_RGBAOrder
  60.  *
  61.  */
  62.  
  63.  
  64. /*
  65.  * This is a sample template for writing new Mesa device drivers.
  66.  * You'll have to rewrite all the pseudo code below.
  67.  *
  68.  * Let's say you're interfacing Mesa to a window/operating system
  69.  * called FOO.  Replace all occurances of FOOMesa with the real name
  70.  * you select for your interface  (i.e. XMesa, WMesa, AmigaMesa).
  71.  *
  72.  * You'll have to design an API for clients to use, defined in a
  73.  * header called Mesa/include/GL/FooMesa.h  Use the sample as an
  74.  * example.  The API should at least have functions for creating
  75.  * rendering contexts, binding rendering contexts to windows/frame
  76.  * buffers, etc.
  77.  *
  78.  * Next, you'll have to write implementations for the device driver
  79.  * functions described in dd.h
  80.  *
  81.  * Note that you'll usually have to flip Y coordinates since Mesa's
  82.  * window coordinates start at the bottom and increase upward.  Most
  83.  * window system's Y-axis increases downward
  84.  *
  85.  * Functions marked OPTIONAL may be completely omitted by your driver.
  86.  *
  87.  * Your Makefile should compile this module along with the rest of
  88.  * the core Mesa library.
  89.  */
  90.  
  91.  
  92. #include <stdlib.h>
  93. #include "GL/mesadriver.h"
  94. #include "context.h"
  95. #include "depth.h"
  96. #include "macros.h"
  97. #include "matrix.h"
  98. #include "types.h"
  99. #include "vb.h"
  100.  
  101. #include "gl/gl.h"
  102. #include "gl/outputhandler.h"
  103. #include "gl/mesamain.h"
  104.  
  105. #include <constructor.h>
  106. #include <intuition/intuition.h>
  107. #include <proto/intuition.h>
  108. #include <proto/graphics.h>
  109. #include <proto/utility.h>
  110. #include <proto/exec.h>
  111. #include <proto/dos.h>
  112.  
  113. #include <m68881.h>
  114. #include <dos.h>
  115.  
  116. #define RGBA(r,g,b,a)    (((r)<<24) | ((g)<<16) | ((b)<<8) | (a))
  117. #define ARGB(r,g,b,a)    (((a)<<24) | ((r)<<16) | ((g)<<8) | (b))
  118.  
  119. #define WINWIDTH(w)        ((w)->Width - (w)->BorderLeft - (w)->BorderRight)
  120. #define WINHEIGHT(w)    ((w)->Height - (w)->BorderTop - (w)->BorderBottom)
  121.  
  122.  
  123. #define QUICKLOOP16(n,l)    { register int ql_var; \
  124.                                 for(ql_var=0; ql_var<((n) & 15); ql_var++) \
  125.                                 { l; }\
  126.                                 for(; ql_var<(n); ql_var+=16) \
  127.                                 { l; l; l; l; l; l; l; l; l; l; l; l; l; l; l; l; } \
  128.                             }
  129.  
  130. #define QUICKLOOP8(n,l)        { register int ql_var; \
  131.                                 for(ql_var=0; ql_var<((n) & 7); ql_var++) \
  132.                                 { l; }\
  133.                                 for(; ql_var<(n); ql_var+=8) \
  134.                                 { l; l; l; l; l; l; l; l; } \
  135.                             }
  136.  
  137. #define QUICKLOOP4(n,l)        { register int ql_var; \
  138.                                 for(ql_var=0; ql_var<((n) & 3); ql_var++) \
  139.                                 { l; }\
  140.                                 for(; ql_var<(n); ql_var+=4) \
  141.                                 { l; l; l; l; } \
  142.                             }
  143.  
  144. #define QUICKLOOP2(n,l)        { register int ql_var; \
  145.                                 for(ql_var=0; ql_var<((n) & 1); ql_var++) \
  146.                                 { l; }\
  147.                                 for(; ql_var<(n); ql_var+=2) \
  148.                                 { l; l; } \
  149.                             }
  150.  
  151. #define QUICKLOOP1(n,l)        { register int ql_var; \
  152.                                 for(ql_var=0; ql_var<(n); ql_var++) \
  153.                                 { l; } \
  154.                             }
  155.  
  156.  
  157. /*
  158.  * This struct contains all device-driver state information.  Think of it
  159.  * as an extension of the core GLcontext from types.h.
  160.  */
  161. struct amiga_mesa_rtl_context {
  162.     GLcontext *gl_ctx;        /* the core library context */
  163.     GLvisual *gl_visual;
  164.     GLframebuffer *gl_buffer;    /* The depth, stencil, accum, etc buffers */
  165.     ULONG mode;                    /* RGB, Index, etc. */
  166.     ULONG rgbaorder;            /* ORDER_RGBA, ORDER_ARGB */
  167.     unsigned long *buffer;        /* The image buffer */
  168.     GLint width, height;        /* Size of image buffer */
  169.     GLint wwidth, wheight;        /* Current output size */
  170.     GLint reqwidth, reqheight;    /* Requester buffer width and height */
  171.     BOOL justcreated;            /* Context has just been created, not yet made current */
  172.     unsigned long pixel;        /* current color index or RGBA pixel value */
  173.     unsigned long clearpixel;    /* pixel for clearing the color buffers */
  174.     struct Library *outputhandler;    /* Output handler library */
  175.     struct Library *mesamain;    /* MesaMain library */
  176.     BOOL myoutputhandler;        /* I opened the output handler */
  177.     ULONG indexpal[256][3];        /* Palette table for index mode */
  178.     ULONG a4;                    /* Global data pointer */
  179.     /* etc... */
  180. };
  181.  
  182.  
  183. /*
  184.  * REMEMBER: These functions may be called in
  185.  *           mesamain's context, NOT mesadriver,
  186.  *           so no automatic access to global data
  187.  */
  188.  
  189.  
  190. static const char *renderer_string(void)
  191. {
  192.     /* No access to global data at A4, put the read-only
  193.      * string in the far section so that we have an
  194.      * absolute reference instead of A4 relative
  195.      */
  196.  
  197.     __far static const char renderer[] = "AmigaMesaRTL";
  198.  
  199.     return renderer;
  200. }
  201.  
  202.  
  203. static void clear_index( GLcontext *ctx, GLuint index )
  204. {
  205.     struct amiga_mesa_rtl_context *amesartl = (struct amiga_mesa_rtl_context *) ctx->DriverCtx;
  206.     /* implement glClearIndex */
  207.     /* usually just save the color index value in the amesartl struct */
  208.     amesartl->clearpixel = index;
  209. }
  210.  
  211.  
  212. static void clear_color_rgba( GLcontext *ctx, GLubyte r, GLubyte g, GLubyte b, GLubyte a )
  213. {
  214.     struct amiga_mesa_rtl_context *amesartl = (struct amiga_mesa_rtl_context *) ctx->DriverCtx;
  215.     /* implement glClearColor */
  216.     /* color components are floats in [0,1] */
  217.     /* usually just save the value in the amesartl struct */
  218.     amesartl->clearpixel = RGBA(r,g,b,a);
  219. }
  220.  
  221.  
  222. static void clear_color_argb( GLcontext *ctx, GLubyte r, GLubyte g, GLubyte b, GLubyte a )
  223. {
  224.     struct amiga_mesa_rtl_context *amesartl = (struct amiga_mesa_rtl_context *) ctx->DriverCtx;
  225.     /* implement glClearColor */
  226.     /* color components are floats in [0,1] */
  227.     /* usually just save the value in the amesartl struct */
  228.     amesartl->clearpixel = ARGB(r,g,b,a);
  229. }
  230.  
  231.  
  232. static void clear( GLcontext *ctx,
  233.            GLboolean all, GLint x, GLint y, GLint width, GLint height )
  234. {
  235.     struct amiga_mesa_rtl_context *amesartl = (struct amiga_mesa_rtl_context *) ctx->DriverCtx;
  236.     unsigned long *bp;
  237.     unsigned char *bbp;
  238.     unsigned long p;
  239. /*
  240.  * Clear the specified region of the current color buffer using the clear
  241.  * color or index as specified by one of the two functions above.
  242.  * If all==GL_TRUE, clear whole buffer, else just clear region defined
  243.  * by x,y,width,height
  244.  */
  245.  
  246.      p = amesartl->clearpixel;
  247.      y = amesartl->wheight-1-y;
  248.  
  249.     if(amesartl->mode == AMRTL_RGBAMode)
  250.     {
  251.         if(all)
  252.         {
  253.             bp = amesartl->buffer;
  254.             QUICKLOOP16(amesartl->width*amesartl->height,
  255.                 {
  256.                     *bp = p;
  257.                     bp++;
  258.                 }
  259.             )
  260.         }
  261.         else
  262.         {
  263.             bp = amesartl->buffer+y*amesartl->width+x;
  264.             QUICKLOOP16(height,
  265.                 {
  266.                     QUICKLOOP16(width,
  267.                         {
  268.                             *bp = p;
  269.                             bp++;
  270.                         }
  271.                     )
  272.                     bp -= amesartl->width + width;
  273.                 }
  274.             )
  275.         }
  276.     }
  277.     else
  278.     {
  279.         if(al